#include "stdafx.h"






/////////////////////////////////////////////////////////////
/*
** Send an HTTP GET request to the Azure Mobile Service data API
** Want whole table
*/
void send_get_request(char * table_name, char * filter) //, char * filter)
{
	// Azure Mobile Service Application Key
	// You can find this key in the 'Manage Keys' menu on the dashboard
	//const char *ams_key = APP_KEY;
	const char *server = AMW_SERVICE;


	output.println("\nConnecting...");
	if (client.connect((char *)server, 80)) {
		output.println(F("sending GET"));

	
		output.print(F("GET /tables/"));
		client.print(F("GET /tables/"));
		output.print(table_name);
		client.print(table_name);
		if (strcmp(filter, "") != 0)
		{
			output.print(F("?"));
			client.print(F("?"));
			output.print(F(filter));
			client.print(F(filter));

		}

		output.println(F(" HTTP/1.1"));
		client.println(F(" HTTP/1.1"));

		// Host header
		//    sprintf(requestBuffer, "Host: %s", server);
		//    output.println(requestBuffer);
		//    client.println(requestBuffer);

		output.print(F("Host: "));
		client.print(F("Host: "));
		output.println(F(AMW_SERVICE));
		client.println(F(AMW_SERVICE));

		// Azure Mobile Services application key
		output.print(F("X-ZUMO-APPLICATION:"));
		client.print(F("X-ZUMO-APPLICATION:"));
		output.println(F(APP_KEY));
		client.println(F(APP_KEY));

		// JSON content type
		output.println(F("Content-Type: application/json"));
		client.println(F("Content-Type: application/json"));

		// POST body


		// Content length
		output.print(F("Content-Length: "));
		output.println(0); //strlen(requestBuffer));
		client.print(F("Content-Length: "));
		client.println(0); //strlen(requestBuffer));


		// End of headers
		output.println();
		client.println();

		// Request body
		output.print(F(""));
		client.print(F(""));

		client.SentHTTPMessage();

	}
	else
	{
		output.println(F("connection failed"));
	}
}


/////////////////////////////////////////////////////////////

extern "C" BOOL GET_TelemetryTable(char * table, bool enableOutput)
{
	return GET_TelemetryTableFltr(table, "embedded=1", enableOutput);
}

/*
** Main Code
*/
extern "C" BOOL GET_TelemetryTableFltr(char * table, char * filter, bool enableOutput)
{
	output.begin(0);
	if (enableOutput)
		output.Enable();
	else
		output.Disable();


	InitNameValues();
	BOOL response = true;

	if (!TEST)
	{
		send_get_request(table, filter); 
	}

	else
	{

		// Sample JSon string
		// Note has extra @ on end
		char * sampleJSonString = "[{\"id\":20,\"sensor\":\"Temperature2\",\"value\":20},{\"id\":12,\"sensor\":\"Temperature2\",\"value\":15}],{\"id\":9,\"sensor\":\"Temperature2\",\"value\":20}]@";
			//"[{\"id\":137,\"value\":9.9,\"sensor\":\"Temperature2\"},{\"id\":138,\"value\":123,\"sensor\":\"Temperature1\"}]@";

		//Use this while loop with sampleJSonString rather than the above 4 request/response calls above  when testing
		int cntr = 0;
		response = true;
		//@ on end of string used here to determine end of string
		while ((sampleJSonString[cntr] != '@') && (response))
		{
			response = ParseJsonString(sampleJSonString[cntr]);
			cntr++;
		}
		CheckArray();
		PrintArray();
	}

	return response;
}


